package org.sxm.cloud.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.sxm.cloud.config.IgnoreUrlsConfig;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * @Description 用户认证全局过滤器，校验用户登录信息
 * @Author xmSheng
 * @Date 2024/3/6
 */
@Component
public class AuthGlobalFilter implements GlobalFilter, Ordered {

    @Resource
    private IgnoreUrlsConfig ignoreUrlsConfig;

    private static final Logger LOG = LoggerFactory.getLogger(AuthGlobalFilter.class);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        final String path = exchange.getRequest().getURI().getPath();
        LOG.info("用户认证校验,请求路径{}",path);
        if(ignoreUrlsConfig.getUrls().contains(path)){ //白名单释放路径
            return chain.filter(exchange);
        }
        return null;
    }

    /**
     * 网关过滤器优先级，数字越低优先级越高
     *
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
